﻿@page
@model SunuerManage.Pages.Manage.Admins.AdminRolesListModel
@{
}

<!-- 主体工具栏 -->

<div class="layui-form" lay-filter="layuiadmin-app-form-list" id="layuiadmin-app-form-list" style="padding: 20px 30px 0 0;">


    <div class="layui-inline">
        <input type="text" id="RolesTitle" name="RolesTitle" class="layui-input" placeholder="角色名" lay-filter="RolesTitle" style="width: 165px;" autocomplete="off">
    </div>
    <button class="layui-btn layuiadmin-btn-list" lay-submit lay-filter="LAY-app-contlist-search">
        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
    </button>
</div>
<div class="layui-card-body">
    <div style="padding-top: 10px;">
        <span id="tongji"></span>
    </div>
</div>
<!-- 主体表格 -->

<table id="LAY-app-content-list" lay-filter="LAY-app-content-list"></table>
<!-- layui -->

<script>
    layui.config({
        base: '/js/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'table', 'laydate', 'layer', 'upload'], function() {

        var table = layui.table, form = layui.form;
        var $ = layui.jquery, upload = layui.upload;
        var laydate = layui.laydate;


        //执行一个日期选择实例
        laydate.render({
            elem: '#stDate'
        });
        //执行一个日期选择实例
        laydate.render({
            elem: '#enDate'
        });
        //监听头工具栏事件
        table.on('toolbar(LAY-app-content-list)', function(obj) {
            var checkStatus = table.checkStatus(obj.config.id)
                , data = checkStatus.data; //获取选中的数据
            switch (obj.event) {
                case 'add':
                    layer.open({
                        type: 2
                        , title: '添加'
                        , content: 'AdminRolesAdd'
                        , maxmin: true
                        , area: ['90%', '90%']
                        , yes: function(index, layero) {
                            //点击确认触发 iframe 内容中的按钮提交
                            var submit = layero.find('iframe').contents().find("#layuiadmin-app-form-submit");
                            submit.click();
                        }
                    });
                    break;
                case 'update':
                    if (data.length === 0) {
                        layer.msg('请选择一行');
                    } else if (data.length > 1) {
                        layer.msg('只能同时编辑一个');
                    } else {
                        layer.open({
                            id: 'bianji',
                            type: 2,
                            title: '编辑',
                            content: 'AdminRolesEdt?RoleID=' + checkStatus.data[0].RoleID,
                            anim: 1,
                            shadeClose: true,
                            shade: 0.8,
                            resize: false,
                            area: ['90%', '90%']
                        });

                    }
                    break;
                case 'delete':
                    if (data.length === 0) {
                        layer.msg('请选择一行');
                    } else {
                        var listdata = '';
                        layer.confirm('确定删除吗？', function(index) {

                            var indexs = layer.load();
                            checkStatus.data.forEach(function(n, i) {
                                listdata = listdata + n.RoleID + ",";
                            });

                            //向服务端发送删除指令
                            $.ajax({
                                url: "/api/AdminRoles/Delete",
                                type: "post",
                                data: { RoleIDs: listdata },
                                dataType: "json",
                                success: function(datax) {
                                    if (0 == datax.code) {
                                       layer.msg(datax.msg, { time: 1000 }, function () {
                                       location.reload()
                                       layer.close(index)
                                    });
                                   } else {
                                  layer.msg(datax.msg, {
                                  icon: 1,
                                  time: 3000 //2秒关闭（如果不配置，默认是3秒）
                                   }, function() {
                                   layer.closeAll();
                                  });
                                 }
                                }
                            })
                            //table.reload('LAY-app-content-list');
                            //layer.msg('已删除');
                        });
                    }
                    break;
            };
        });
        //监听搜索
        form.on('submit(LAY-app-contlist-search)', function(data) {
            var field = data.field;
            //执行重载
            table.reload('LAY-app-content-list', {
                where: field
                , page: {
                    curr: 1 //重新从第 1 页开始
                }
                , limit: 15
                , limits: [15, 15, 20, 25, 30]
                , text: { none: '无数据' }
            });

        });

        //文章管理
        table.render({
            elem: '#LAY-app-content-list'
            , toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
            , defaultToolbar: []
            , url: '/api/AdminRoles/Get' //模拟接口
            , cols: [[
                { type: 'checkbox', fixed: 'left', width: 80 }
                ,
                { field: 'RoleID', width: 100, title: 'ID', sort: true }
                , { field: 'RolesTitle', title: '角色名称' }
                , { title: '操作', width: 150, align: 'center', fixed: 'right', toolbar: '#table-content-list' }
            ]], request: {
                pageName: 'Page', //页码的参数名称
                limitName: 'PageSize' //每页数据量的参数名
            }
            , page: true
            , limit: 15
            , limits: [10, 15, 20, 25, 30, 300]
            , text: '对不起，加载出现异常！'
            , done: function(res, curr, count) {
                $("#total").html(count);
            }, text: { none: '无数据' }
        });
        //监听工具条
        table.on('tool(LAY-app-content-list)', function(obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                var ids = data.RoleID;
                layer.open({
                    id: 'bianji',
                    type: 2,
                    title: '编辑',
                    content: 'AdminRolesEdt?RoleID=' + ids,
                    anim: 1,
                    shadeClose: true,
                    shade: 0.8,
                    resize: false,
                    area: ['90%', '90%']
                });

            } else if (obj.event === 'del') {
                layer.confirm('确定删除吗？', function(index) {
                    var indexs = layer.load();
                    var RoleID = data.RoleID;
                    //向服务端发送删除指令
                    $.ajax({
                        url: "/api/AdminRoles/Delete",
                        type: "post",
                        data: { RoleIDs: RoleID + "," },
                        dataType: "json",
                        success: function(datax) {
                            if (0 == datax.code) {
                            layer.msg(datax.msg, { time: 1000 }, function () {
                            location.reload()
                            layer.close(index)
                            });
                            } else {
                            layer.msg(datax.msg, {
                            icon: 1,
                                time: 3000 //2秒关闭（如果不配置，默认是3秒）
                             }, function() {
                            layer.closeAll();
                            });
                            }
                        }
                    })
                });
            }
        });

    });

    function GetDateFormat(str) {
        return new Date(parseInt(str.replace("/Date(", "").replace(")/", ""), 10)).toLocaleString();
    }
</script>

<!-- 数据模版 -->
<script type="text/html" id="CreateDates">
    {{ toDate(d.CreateDate).pattern("yyyy-MM-dd") }}
</script>

<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        @if (Model.AdminModel.Roles.IndexOf("|6|") > -1)
        {
                  <button class="layui-btn layuiadmin-btn-list " lay-event="add"><i class="layui-icon layui-icon-add-1"></i>添加</button>
        }
        @if (Model.AdminModel.Roles.IndexOf("|7|") > -1)
        {
                  <button class="layui-btn layuiadmin-btn-list " lay-event="update"><i class="layui-icon layui-icon-edit"></i>编辑</button>
        }
        @if (Model.AdminModel.Roles.IndexOf("|8|") > -1)
        {
                  <button  class="layui-btn layuiadmin-btn-list" lay-event="delete"><i class="layui-icon layui-icon-delete"></i>删除</button>
        }
    </div>
</script>

<script type="text/html" id="table-content-list">
    @if (Model.AdminModel.Roles.IndexOf("|8|") > -1)
    {
                 <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-set"></i>编辑</a>
    }

    @if (Model.AdminModel.Roles.IndexOf("|9|") > -1)
    {
                    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
    }
</script>